<template>
  <div
    class="min-h-screen bg-gradient-to-br from-gray-50 via-blue-50 to-indigo-50 dark:from-gray-900 dark:via-gray-800 dark:to-gray-900"
  >
    <!-- 页面头部 -->
    <div
      class="relative bg-white/80 dark:bg-gray-800/80 backdrop-blur-sm shadow-lg border-b border-gray-200/50 dark:border-gray-700/50"
    >
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <div class="text-center mb-10">
          <h1
            class="text-4xl sm:text-5xl font-bold bg-gradient-to-r from-blue-600 via-indigo-600 to-purple-600 bg-clip-text text-transparent mb-4"
          >
            知识库
          </h1>
          <p class="text-xl text-gray-600 dark:text-gray-300 max-w-3xl mx-auto leading-relaxed">
            探索丰富的技术知识，发现实用的开发资源，构建你的技术知识体系
          </p>
        </div>

        <!-- 搜索框 -->
        <div class="max-w-2xl mx-auto relative">
          <div class="relative group">
            <div
              class="absolute inset-0 bg-gradient-to-r from-blue-500 to-indigo-500 rounded-2xl blur opacity-20 group-hover:opacity-30 transition-opacity duration-300"
            ></div>
            <div class="relative">
              <svg
                class="absolute left-5 top-1/2 -translate-y-1/2 w-6 h-6 text-gray-400 dark:text-gray-500 pointer-events-none transition-colors duration-200"
                xmlns="http://www.w3.org/2000/svg"
                fill="none"
                viewBox="0 0 24 24"
                stroke="currentColor"
              >
                <path
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  stroke-width="2"
                  d="M21 21l-4.35-4.35M17 11a6 6 0 11-12 0 6 6 0 0112 0z"
                />
              </svg>
              <input
                v-model="param.title"
                type="text"
                placeholder="搜索知识库标题..."
                @input="onSearch"
                class="w-full pl-16 pr-6 py-5 text-lg rounded-2xl shadow-xl border-0 bg-white/90 dark:bg-gray-700/90 backdrop-blur-sm text-gray-800 dark:text-white placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring-4 focus:ring-blue-500/30 dark:focus:ring-blue-400/30 transition-all duration-300 hover:shadow-2xl"
              />
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
      <!-- 加载状态 -->
      <div
        v-if="loading"
        class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8"
      >
        <div v-for="i in 8" :key="i" class="animate-pulse">
          <div
            class="bg-white/80 dark:bg-gray-800/80 backdrop-blur-sm rounded-3xl overflow-hidden shadow-xl border border-gray-200/20 dark:border-gray-700/20"
          >
            <div
              class="aspect-[9/6] bg-gradient-to-br from-gray-200 to-gray-300 dark:from-gray-700 dark:to-gray-600"
            ></div>
            <div class="p-6 space-y-4">
              <div
                class="h-7 bg-gradient-to-r from-gray-200 to-gray-300 dark:from-gray-700 dark:to-gray-600 rounded-lg"
              ></div>
              <div
                class="h-5 bg-gradient-to-r from-gray-200 to-gray-300 dark:from-gray-700 dark:to-gray-600 rounded-lg"
              ></div>
              <div
                class="h-5 bg-gradient-to-r from-gray-200 to-gray-300 dark:from-gray-700 dark:to-gray-600 rounded-lg w-3/4"
              ></div>
              <div
                class="h-5 bg-gradient-to-r from-gray-200 to-gray-300 dark:from-gray-700 dark:to-gray-600 rounded-lg w-1/2"
              ></div>
            </div>
          </div>
        </div>
      </div>

      <!-- 知识库卡片网格 -->
      <div
        v-else-if="!loading && knowledgeList.length > 0"
        class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8"
      >
        <WikiCard
          v-for="item in knowledgeList"
          :key="item.id"
          :data="item"
          class="transition-all duration-500 hover:scale-105 hover:-translate-y-2"
        />
      </div>

      <!-- 空状态提示 -->
      <div v-else-if="!loading && knowledgeList.length === 0" class="text-center py-20">
        <div class="relative mb-8">
          <div
            class="absolute inset-0 bg-gradient-to-r from-blue-400 to-indigo-400 rounded-full blur-3xl opacity-20"
          ></div>
          <div class="relative">
            <svg
              class="mx-auto h-24 w-24 text-gray-300 dark:text-gray-600"
              fill="none"
              viewBox="0 0 24 24"
              stroke="currentColor"
            >
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="1"
                d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
              />
            </svg>
          </div>
        </div>
        <h3 class="text-2xl font-bold text-gray-700 dark:text-gray-200 mb-3">暂无知识库</h3>
        <p class="text-lg text-gray-500 dark:text-gray-400 max-w-md mx-auto">
          没有找到匹配的知识库内容，尝试调整搜索关键词
        </p>
      </div>

      <!-- 分页控件 -->
      <div v-if="!loading && knowledgeList.length > 0" class="mt-16 flex justify-center">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          v-model:current-page="pagination.currentPage"
          :page-size="pagination.pageSize"
          :total="pagination.total"
          @current-change="handlePageChange"
          class="custom-pagination"
          :hide-on-single-page="true"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import 'element-plus/dist/index.css'
import { getWikiPage } from '@/apis/blog/wiki'
import WikiCard from './WikiCard.vue'
const currentPage = ref(1)

const handlePageChange = page => {
  currentPage.value = page
  onSearch()
}

const param = ref({
  title: undefined
})
const pagination = reactive({
  total: 10,
  pageSize: 4,
  currentPage: 1
})

async function onSearch() {
  const res = await getWikiPage({
    currentPage: pagination.currentPage,
    pageSize: pagination.pageSize,
    title: param.title
  })
  knowledgeList.value = res.list
  pagination.total = res.total
}
onMounted(async () => {
  onSearch()
})
const knowledgeList = ref([])
</script>

<style scoped>
.custom-pagination {
  --el-pagination-bg-color: transparent;
  --el-pagination-text-color: #6b7280;
  --el-pagination-hover-color: #3b82f6;
  --el-pagination-active-bg-color: #3b82f6;
  --el-pagination-active-text-color: #ffffff;
}

:deep(.el-pagination .el-pager li) {
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(229, 231, 235, 0.5);
  color: #6b7280;
  transition: all 0.3s ease;
  border-radius: 12px;
  margin: 0 4px;
  min-width: 40px;
  height: 40px;
  line-height: 40px;
  font-weight: 500;
}

:deep(.el-pagination .el-pager li:hover) {
  background-color: rgba(59, 130, 246, 0.1);
  border-color: #3b82f6;
  color: #3b82f6;
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(59, 130, 246, 0.15);
}

:deep(.el-pagination .el-pager li.is-active) {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  border-color: #3b82f6;
  color: #ffffff;
  box-shadow: 0 8px 25px rgba(59, 130, 246, 0.3);
  transform: translateY(-2px);
}

:deep(.el-pagination .btn-prev),
:deep(.el-pagination .btn-next) {
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(229, 231, 235, 0.5);
  color: #6b7280;
  transition: all 0.3s ease;
  border-radius: 12px;
  margin: 0 8px;
  min-width: 40px;
  height: 40px;
  line-height: 40px;
}

:deep(.el-pagination .btn-prev:hover),
:deep(.el-pagination .btn-next:hover) {
  background-color: rgba(59, 130, 246, 0.1);
  border-color: #3b82f6;
  color: #3b82f6;
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(59, 130, 246, 0.15);
}

:deep(.el-pagination .el-pager .more) {
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(229, 231, 235, 0.5);
  border-radius: 12px;
  margin: 0 4px;
}

/* 响应式调整 */
@media (max-width: 640px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
    gap: 1.5rem;
  }

  :deep(.el-pagination .el-pager li),
  :deep(.el-pagination .btn-prev),
  :deep(.el-pagination .btn-next) {
    min-width: 36px;
    height: 36px;
    line-height: 36px;
    font-size: 14px;
  }
}

@media (min-width: 641px) and (max-width: 1024px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 2rem;
  }
}

@media (min-width: 1025px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 2rem;
  }
}

@media (min-width: 1280px) {
  .grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 2rem;
  }
}
</style>
